package com.whw.springboot.walk.controller;


import com.whw.springboot.walk.UseToken;
import com.whw.springboot.walk.config.GlobalConfig;
import com.whw.springboot.walk.entity.Action;
import com.whw.springboot.walk.entity.ActionNumber;
import com.whw.springboot.walk.entity.Message;
import com.whw.springboot.walk.service.ActionService;
import com.whw.springboot.walk.utils.ApplyUtil;
import com.whw.springboot.walk.utils.LoadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@Api(tags = "活动模块")
@RestController
@RequestMapping("/api/action")
public class ActionController {

    @Resource
    private ActionService actionService;
    @Resource
    private GlobalConfig globalConfig;

    @ApiOperation(value = "添加活动")
    @UseToken(level = 0)
    @PostMapping(value = "/insert")//, headers = "content-type=multipart/form-data")
    public Message insertAction(ActionNumber actionNumber) throws ParseException {
        String actId = "AC"+ApplyUtil.genApplyId();
        actionNumber.setActId(actId);
        Action action = new Action();
        action.setActTitleImg(actionNumber.getActTitleImg());
        return actionService.insertAction(actionNumber);
    }


    @ApiOperation(value="上传图片")
    @PostMapping(value="/Images" , headers = "content-type=multipart/form-data")
    @UseToken(level = 0)
    public String getImages(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request) throws IOException {
        return globalConfig.hostUrl+"resource/"+LoadUtil.upload(globalConfig.filePath,files, request);

    }


    @ApiOperation(value = "通过活动id删除活动")
    @DeleteMapping(value = "/delByActId")
    @UseToken(level = 1)
    public Message delAction(String actId){
        return actionService.deleteActionByPrimaryKey(actId);
    }

    @ApiOperation(value = "通过活动id更新活动")
    @PutMapping(value = "/updateAction")
    @UseToken(level = 0)
    public Message updateAction(ActionNumber actionNumber) throws ParseException {
        return actionService.updateActionByPrimaryKey(actionNumber);
    }

    @ApiOperation(value = "通过活动id查看活动")
    @GetMapping(value = "/findByActId")
    @UseToken(level = 0)
    public Message<Action> findActionByPrimaryKey(String actId){
        return  actionService.findActionByPrimaryKey(actId);
    }

    @ApiOperation(value = "查看所有的活动")
    @UseToken(level = 1)
    @GetMapping(value = "/all")
    public Message<List<Action>> findAllAction(){
        return actionService.findAllActions();
    }

    @ApiOperation(value = "通过活动类型查看活动")
    @UseToken(level = 0)
    @GetMapping(value = "/findByType")
    public Message<List<Action>> findActionByType(String actType){
        return actionService.findActionsByType(actType);
    }

    @ApiOperation(value = "通过活动标题查看活动")
    @UseToken(level = 0)
    @GetMapping(value = "/findByTitle")
    public Message<List<Action>> findActionByTitle(String actTitle){
        return actionService.findActionByTitle(actTitle);
    }

    @ApiOperation(value = "管理端审核活动，需要传actId,actStatus")
    @UseToken(level = 1)
    @PutMapping(value = "/adminManageAction")
    public Message adminManageAction(ActionNumber actionNumber) throws ParseException {
        return actionService.adminManageAction(actionNumber);
    }

    @ApiOperation(value = "查看某个用户参加的所有的活动")
    @GetMapping(value = "/findoneuseraction")
    public Message<List<Action>> findOneUserActions(String userId){
        return actionService.findOneUserAllActions(userId);
    }
}
